//    Openbravo POS is a point of sales application designed for touch screens.
//    Copyright (C) 2008-2009 Openbravo, S.L.
//    http://www.openbravo.com/product/pos
//
//    This file is part of Openbravo POS.
//
//    Openbravo POS is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Openbravo POS is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Openbravo POS.  If not, see <http://www.gnu.org/licenses/>.
package com.openbravo.pos.sales.restaurant;

import com.openbravo.bean.Place;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import javax.swing.event.ListSelectionListener;
import java.util.logging.Logger;
import javax.swing.AbstractListModel;

public class JPlaceList extends javax.swing.JPanel {

    private static Logger logger = Logger.getLogger("com.openbravo.pos.sales.JTicketLines");
    private PlaceListModel m_jTableModel;

    /** Creates new form JLinesTicket */
    public JPlaceList() {

        initComponents();
        m_jTableModel = new PlaceListModel();
        m_jPlaceList.setModel(m_jTableModel);

        m_jScrollListTicket.getVerticalScrollBar().setPreferredSize(new Dimension(35, 35));
    }

    public void addListSelectionListener(ListSelectionListener l) {
        m_jPlaceList.getSelectionModel().addListSelectionListener(l);
    }

    public void removeListSelectionListener(ListSelectionListener l) {
        m_jPlaceList.getSelectionModel().removeListSelectionListener(l);
    }

    public void clearPlaces() {
        m_jTableModel.clear();
    }

    public void addPlace(Place oLine) {

        m_jTableModel.addRow(oLine);

        // Selecciono la que acabamos de anadir.            
        setSelectedIndex(m_jTableModel.getSize() - 1);
    }

     public void addSelectedLines(Place[] lines) {
            // add all at the end in inverse order.
           for(int i=0;i<lines.length;i++){
               m_jTableModel.addRow(lines[i]);
           }
    }

    public void insertPlace(int index, Place oPlace) {

        m_jTableModel.insertRow(index, oPlace);

        // Selecciono la que acabamos de anadir.            
        setSelectedIndex(index);
    }

    public void removePlace(int i) {

        m_jTableModel.removeRow(i);

        // Escojo una a seleccionar
        if (i >= m_jTableModel.getSize()) {
            i = m_jTableModel.getSize() - 1;
        }

        if ((i >= 0) && (i < m_jTableModel.getSize())) {
            // Solo seleccionamos si podemos.
            setSelectedIndex(i);
        }
    }

    public void setSelectedIndex(int i) {
        // Seleccionamos
        m_jPlaceList.getSelectionModel().setSelectionInterval(i, i);
    }

    public int getSelectedIndex() {
        return m_jPlaceList.getSelectionModel().getMinSelectionIndex(); // solo sera uno, luego no importa...
    }


    public Place[] getSelectedObjects(){
        List objs=m_jPlaceList.getSelectedValuesList();
        return (Place[]) objs.toArray();
    }

    public void selectionDown() {

        int i = m_jPlaceList.getSelectionModel().getMaxSelectionIndex();
        if (i < 0) {
            i = 0; // No hay ninguna seleccionada
        } else {
            i++;
            if (i >= m_jTableModel.getSize()) {
                i = m_jTableModel.getSize() - 1;
            }
        }

        if ((i >= 0) && (i < m_jTableModel.getSize())) {
            // Solo seleccionamos si podemos.

            setSelectedIndex(i);
        }
    }

    public void selectionUp() {

        int i = m_jPlaceList.getSelectionModel().getMinSelectionIndex();
        if (i < 0) {
            i = m_jTableModel.getSize() - 1; // No hay ninguna seleccionada
        } else {
            i--;
            if (i < 0) {
                i = 0;
            }
        }

        if ((i >= 0) && (i < m_jTableModel.getSize())) {
            // Solo seleccionamos si podemos.
            setSelectedIndex(i);
        }
    }

    public void reflush(List<Place> m_rows){
        m_jTableModel.setM_rows(m_rows);
    }

    public List<Place> getAll(){
        return  m_jTableModel.getM_rows();
    }

    void removeLines(Place[] lines) {
        m_jTableModel.removeObjects(lines);
    }


    public static class PlaceListModel extends AbstractListModel {
        private List<Place> m_rows;

        public PlaceListModel() {
              m_rows=new ArrayList<Place>();
        }

        public void clear() {
            int old = getSize();
            if (old > 0) {
                m_rows.clear();
                fireIntervalRemoved(this, 0, old - 1);
            }
        }

        public void addRow(Place oLine) {
            insertRow(m_rows.size(), oLine);
        }

        public void insertRow(int index, Place oLine) {
            m_rows.add(index, oLine);
            fireIntervalAdded(oLine, index, index);
        }

        public void removeRow(int row) {
            Object removeObj=m_rows.get(row);
            m_rows.remove(row);
            fireIntervalRemoved(removeObj, row, row);
        }

        @Override
        public int getSize() {
            return m_rows.size();
        }

        public List<Place> getM_rows() {
            return m_rows;
        }

        public void setM_rows(List<Place> m_rows) {
            int old = getSize();
            this.m_rows = m_rows==null?new ArrayList<Place>():m_rows;
            fireContentsChanged(this, 0, old-1);
        }

        @Override
        public Object getElementAt(int index) {
            return m_rows.get(index);
        }

        private void removeObjects(Place[] lines) {

            for(Place place:lines){
               m_rows.remove(place);
           }
           fireContentsChanged(this, 0, m_rows.size()-1);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        m_jScrollListTicket = new javax.swing.JScrollPane();
        m_jPlaceList = new javax.swing.JList();

        setLayout(new java.awt.BorderLayout());

        m_jPlaceList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        m_jScrollListTicket.setViewportView(m_jPlaceList);

        add(m_jScrollListTicket, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList m_jPlaceList;
    private javax.swing.JScrollPane m_jScrollListTicket;
    // End of variables declaration//GEN-END:variables
}
